import {
    getCrouseList,
} from '../../api/article'
import {
    getId,
    submit,
    pay
} from '../../api/user'
// pages/crouse/crouse.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        lastPage: false,
        params: {
            pageIndex: 1,
            pageSize: 10,
            state: 1
        },
        list: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.init()
    },
    async init() {

        const res = await getCrouseList(this.data.params)

        this.setData({
            list: this.data.list.concat(res.content),
            lastPage: res.lastPage,
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.data.params.pageIndex = 1
        this.setData({
            list: [],
            lastPage: false
        })
        this.init()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (!this.data.lastPage) {
            this.setData({
                ['params.pageIndex']: this.data.params.pageIndex += 1
            })
            this.init()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    async handleSubmit(e) {
        if (!wx.getStorageSync('token')) {
            wx.showModal({
                title: '提示',
                content: '请登录后购买',
                complete: (res) => {
                    if (res.cancel) {

                    }
                    if (res.confirm) {
                        wx.switchTab({
                            url: '/pages/my/my',
                        })
                    }
                }
            })
            return
        }
        const ids = await getId({
            type: 2
        })
        const res = await submit({
            goodsId: e.currentTarget.dataset.item.courseId,
            num: 1,
            orderId: ids.content
        })
        const payres = await pay({
            orderId: ids.content
        })
        if (payres.content) {
            wx.requestPayment({
                nonceStr: payres.content.nonce_str,
                package: payres.content.package,
                paySign: payres.content.sign,
                signType: 'MD5',
                timeStamp: payres.content.timestamp,
                success(res) {
                    console.log(res)
                    wx.showToast({
                        title: '支付成功',
                        icon: 'success'
                    })
                },
                fail(err) {
                    console.log(err)
                    wx.showToast({
                        title: '支付失败',
                        icon: 'error'
                    })
                }
            })
        }
    },

    handlePlay(e) {
        if (!wx.getStorageSync('token')) {
            wx.showModal({
                title: '提示',
                content: '请登录',
                complete: (res) => {
                    if (res.cancel) {

                    }
                    if (res.confirm) {
                        wx.switchTab({
                            url: '/pages/my/my',
                        })
                    }
                }
            })
            return
        }
        wx.navigateTo({
            url: `/pages/paly/paly?id=${e.currentTarget.dataset.item.courseId}`,
        })
    }
})